import Vue from "vue";
import VueRouter from "vue-router";
import AddGoods from "@/pages/AddGoods.vue";
import GoodsList from "@/pages/GoodsList.vue";
import MyCart from "@/pages/MyCart.vue";
import Login from "@/pages/Login.vue";
Vue.use(VueRouter);
const router = new VueRouter({
	routes:[
		{
			path:"/",
			component:AddGoods
		},{
			path:"/goodsList",
			component:GoodsList
		},{
			path:"/myCart",
			component:MyCart,
			meta:{
				isAuthorization:true
			}
		},{
			path:"/login",
			meta:{
				hideNav:true
			},
			component:Login
		}
	],
	mode:"history"
});
// 全局前置守卫。
router.beforeEach(function(to,from,next){
	// 查看要去的路由是否需要验证身份权限
	if(to.meta.isAuthorization){
		if(localStorage.getItem("token")){
			next();
		}else{
			next({
				path:"/login",
				returnUrl:to.fullPath
			})
		}
	}else{
		next();
	}
})
export default router;